package com.hxc.shop.controller;

import com.hxc.shop.bean.ShopCart;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/cart")
@CrossOrigin
@Api(tags = {"购物车数据处理"})
public class CartController {
    @Resource
    private CartService cartService;

    @ApiOperation("获取购物车数据")
    @GetMapping("/getCartInfoByUserName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户名",required = true,paramType = "String")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO getCartInfoByUserName(@RequestParam("userName")String userName){
        return cartService.getCartInfoByUserName(userName);
    }

    @ApiOperation("新增购物车数据")
    @PostMapping("/insertCartInfo")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO insertCartInfo(@RequestBody ShopCart cart){
        return cartService.insertCartInfo(cart);
    }

    @ApiOperation("修改购物车商品数据")
    @PostMapping("/updateCartInfo")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO updateCartInfo(@RequestBody ShopCart cart){
        return cartService.updateCartInfo(cart);
    }

    @ApiOperation("删除购物车商品数据")
    @GetMapping("/deleteCartInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户名",required = true,paramType = "String"),
            @ApiImplicitParam(name = "goodsId",value = "商品编号",required = true,paramType = "Integer"),
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO deleteCartInfo(@RequestParam("userName")String userName,@RequestParam("goodsId")Integer goodsId){
        return cartService.deleteCartInfo(userName,goodsId);
    }

    @ApiOperation("根据商品名称查找购物车数据")
    @GetMapping("/getCartGoodsByGoodsName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName",value = "商品名称",required = true,paramType = "String"),
            @ApiImplicitParam(name = "userName",value = "用户名",required = true,paramType = "String"),
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO getCartGoodsByGoodsName(@RequestParam("goodsName")String goodsName,@RequestParam("userName")String userName) {
        return cartService.getCartGoodsByGoodsName(goodsName,userName);
    }

}
